class Solution {
public:
    unordered_map<Node*,Node*> visited;
    Node* cloneGraph(Node* node) {
        if(node == NULL){
            return NULL;
        }
        
        if(visited.count(node)){
            return visited[node];
        }
        
        Node* newNode = new Node(node->val);
        
        visited[node] = newNode;
        
        for(auto& vc : node->neighbors){
            newNode->neighbors.push_back(cloneGraph(vc));
        }
        
        return newNode;
    }
};
